/**
 * 
 */
package v1c07;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;

import javax.swing.JComponent;
import javax.swing.JFrame;

/**
 * @author gjunming@gmail.com
 * @version Aug 6, 2009 1:29:22 PM
 */
public class DrawFrameTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {

			@Override
			public void run() {
				DrawFrame myDrawFrame = new DrawFrame();
				myDrawFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				myDrawFrame.setVisible(true);
			}
		});
	}
}

class DrawFrame extends JFrame {
	private static final long serialVersionUID = 1L;

	public DrawFrame() {
		setSize(600, 400);
		setTitle("hello world");

		DrawComponent myDrawComponent = new DrawComponent();
		add(myDrawComponent);

	}
}

class DrawComponent extends JComponent {

	private static final long serialVersionUID = 1L;

	@Override
	public void paintComponent(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;
		
		//set default rectangle size
		Double leftX = 300.0;
		Double topY = 200.0;
		Double width = 300.0;
		Double height = 200.0;

		// a rectangle
		Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width, height);
		g2.draw(rect);

		// a ellipse closed to inner rectangle
		Ellipse2D ellRect = new Ellipse2D.Double();
		// ellRect.setFrame(rect);
		ellRect.setFrameFromCenter(leftX, topY, width / 2, height / 2);
		g2.draw(ellRect);
		
		// a line
		Line2D line = new Line2D.Double(leftX, topY, leftX + width, topY
				+ height);
		g2.draw(line);

		// set paint ( 画笔)color
		g2.setPaint(Color.CYAN);
		// set font
		Font myFont = new Font("SansSerif", Font.BOLD + Font.ITALIC, 35);
		g2.setFont(myFont);
		g2.drawString("hello world", 150, 100);
	}
}